import Vue from 'vue'
import VueRouter from 'vue-router'
// import Login from 'components/common/Login.vue'
// import Home from 'views/home/Home.vue'
// import Welcome from 'views/home/childComps/Welcome.vue'

// import Users from 'views/home/childComps/users/Users.vue'
// import Rights from 'views/home/childComps/rights/Rights.vue'
// import Roles from 'views/home/childComps/rights/Roles.vue'
// import Goods from 'views/home/childComps/goods/Goods.vue'
// import Params from 'views/home/childComps/goods/Params.vue'
// import Cate from 'views/home/childComps/goods/Cate.vue'
// import Add from 'views/home/childComps/goods/Add.vue'
// import Orders from 'views/home/childComps/orders/Orders.vue'
// import Reports from 'views/home/childComps/reports/Report.vue'

//路由懒加载
const Login = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ 'components/common/Login.vue')
const Home = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ 'views/home/Home.vue')
const Welcome = () =>
    import ( /* webpackChunkName:"login_home_welcome" */ 'views/home/childComps/Welcome.vue')
const Users = () =>
    import ( /* webpackChunkName:"Users_Rights_Roles" */ 'views/home/childComps/users/Users.vue')
const Rights = () =>
    import ( /* webpackChunkName:"Users_Rights_Roles" */ 'views/home/childComps/rights/Rights.vue')
const Roles = () =>
    import ( /* webpackChunkName:"Users_Rights_Roles" */ 'views/home/childComps/rights/Roles.vue')
const Goods = () =>
    import ( /* webpackChunkName:"Goods_Params" */ 'views/home/childComps/goods/Goods.vue')
const Params = () =>
    import ( /* webpackChunkName:"Goods_Params_Cate" */ 'views/home/childComps/goods/Params.vue')
const Cate = () =>
    import ( /* webpackChunkName:"Goods_Params_Cate" */ 'views/home/childComps/goods/Cate.vue')
const Add = () =>
    import ( /* webpackChunkName:"Add_Orders_Reports" */ 'views/home/childComps/goods/Add.vue')
const Orders = () =>
    import ( /* webpackChunkName:"Add_Orders_Reports" */ 'views/home/childComps/orders/Orders.vue')
const Reports = () =>
    import ( /* webpackChunkName:"Add_Orders_Reports" */ 'views/home/childComps/reports/Report.vue')
    //挂载路由
Vue.use(VueRouter)
    //初始化路由
const routes = [{
        //重定向路由、配置默认路由
        path: '/',
        redirect: '/login'
    },
    //配置跳转路由映射
    {
        path: '/login',
        name: 'Login',
        component: Login,
        //可以在配置时导入
        // component: () =>
        //     import ( /* webpackChunkName: "about" */ '../views/About.vue')
    },
    {
        path: '/home',
        name: 'Home',
        component: Home,
        //重定向
        redirect: '/welcome',
        //子路由
        children: [{
                path: '/welcome',
                name: 'Welcome',
                component: Welcome
            },
            {
                path: '/users',
                name: 'Users',
                component: Users
            },
            {
                path: '/rights',
                name: 'Rights',
                component: Rights
            },
            {
                path: '/roles',
                name: 'Roles',
                component: Roles
            },
            {
                path: '/goods',
                name: 'Goods',
                component: Goods,

            },
            {
                path: '/params',
                name: 'Params',
                component: Params
            },
            {
                path: '/categories',
                name: 'Cate',
                component: Cate
            },
            {
                path: '/goods/add',
                name: 'Add',
                component: Add,
            },
            {
                path: '/orders',
                name: 'Orders',
                component: Orders,
            },
            {
                path: '/reports',
                name: 'Reports',
                component: Reports,
            }
        ]
    },

]

//创建路由实例
const router = new VueRouter({
    routes
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
    //to 将要访问的路径
    //reom 代表从哪个庐江跳转过来
    //next是一个函数,表示放行
    if (to.path === '/login') return next();
    const tokenStr = window.sessionStorage.getItem('token')
        //如果没有token,强制跳转路由到登录界面
    if (!tokenStr) return next('/login')
    next()

})

export default router